Comandes générales et visualisation bivariée

Python works with packages. A large number of packages can be dowloaded all together in the Anaconda environment. Start downloading here (version >3) https://www.anaconda.com/download/ Anaconda comes with

  • Spyder, a GUI for python
  • Jupyter Notebook, how to create great documents

Enjoy!

Bee careful :

  • Python is sensible to spaces in the beginning of lines,
  • Python is sensible to number type : $3$ and $3.$ are totally different: integer and float types.
In [1]:
import warnings
warnings.filterwarnings('ignore')

1 Handle basic instructions

In [2]:
print(1+2) # SUM #First operation and first comment
print(2*3) # MULTIPLICATION #Second operation and second comment
#Third comment
print(4/5) # DIVISION
print(3**4)# RAISE POWER
print((25/6-2*3)**2) # COMPLEXE OPERATION
print("Hello World")
3
6
0.8
81
3.36111111111111
Hello World

2 The Variables

2.1 Create the variables

In [3]:
my_integer = 54213
my_float = 21.514302
my_string = "Hello World"

2.2 Check the variables

In [4]:
print(my_integer)
print(my_float)
print(my_string)
54213
21.514302
Hello World

2.3 Test the variables

integer against float

In [5]:
print(my_integer > my_float)
print(my_integer != my_float)
print(my_integer == my_float)
print(my_integer < my_float)
True
True
False
False

integer against string

Those which work...
In [6]:
print(my_integer != my_string)
print(my_integer == my_string)
True
False
... and those which don't
In [7]:
# print(my_integer > my_string)
# print(my_integer < my_string)

3 The Python one-dimensional structures

3.1 Lists

Contain items separated by commas and enclosed within square brackets $[]$.
Each element can be modified.

In [8]:
my_list = [54213,21.514302,"Hello World","EDO"]
my_second_list = ["Ecole","Sante",2018,"EDO"]

print(my_list)
print(my_list[0])
print(my_list[1:3])
print(my_list[2:])
print(my_second_list*2)
print(my_list + my_second_list)

my_third_list = my_list[2:]
my_third_list[1] = "On change le deuxième élément"
print(my_third_list)
[54213, 21.514302, 'Hello World', 'EDO']
54213
[21.514302, 'Hello World']
['Hello World', 'EDO']
['Ecole', 'Sante', 2018, 'EDO', 'Ecole', 'Sante', 2018, 'EDO']
[54213, 21.514302, 'Hello World', 'EDO', 'Ecole', 'Sante', 2018, 'EDO']
['Hello World', 'On change le deuxième élément']

3.2 Tuples

Contain items separated by commas and enclosed within square brackets $()$.
NO element can be modified.

In [9]:
my_tuple = (54213,21.514302,"Hello World","EDO")
my_second_tuple = ("Ecole","Sante",2018,"EDO")

print(my_tuple)
print(my_tuple[0])
print(my_tuple[1:3])
print(my_tuple[2:])
print(my_second_tuple*2)
print(my_tuple + my_second_tuple)

my_third_tuple = my_tuple[2:]
# my_third_tuple[1] = "On change le deuxième élément"    # --> A tester... erreur!
print(my_third_tuple)
(54213, 21.514302, 'Hello World', 'EDO')
54213
(21.514302, 'Hello World')
('Hello World', 'EDO')
('Ecole', 'Sante', 2018, 'EDO', 'Ecole', 'Sante', 2018, 'EDO')
(54213, 21.514302, 'Hello World', 'EDO', 'Ecole', 'Sante', 2018, 'EDO')
('Hello World', 'EDO')

3.3 Dictionnaries

Works on the principle of key-value.
Dictionaries are enclosed by curly braces ${ }$ and values can be assigned and accessed using square braces $[]$.

In [10]:
my_dict = {}
my_dict['one'] = "un en inglish"
my_dict[2]     = "deux"

my_second_dict = {'nom': 'hadrien','age':28, 'taille':1.94}


print(my_dict['one'])
print(my_dict[2])
print(my_second_dict)
print(my_second_dict.keys())
print(my_second_dict.values())
un en inglish
deux
{'nom': 'hadrien', 'age': 28, 'taille': 1.94}
dict_keys(['nom', 'age', 'taille'])
dict_values(['hadrien', 28, 1.94])

3.4 Strings

One acceses to strings values directly using using square braces $[]$.

In [11]:
my_string = "Hello Le Monde"
my_second_string = "Comment ca va ?"

print(my_second_string[0:7] + " donc" + my_second_string[10:14] + my_string[6:14] + my_second_string[-1])
Comment donc va Le Monde?

3.5 String operations

Very practical to generate automatic personnalized titles in figures.

In [12]:
print("My name is %s and my weight is %d kg! My father's weight is %d kg but he is older than me!" % ('Zara', 21,95))
My name is Zara and my weight is 21 kg! My father's weight is 95 kg but he is older than me!

4 The mathematics and Python

4.1 Generate a list of integer or float values

The notation is range(starting value, end value+1, step). The step parameter is optionnal.

In [13]:
my_list = list(range(1,10,1))
print(my_list)
# print(math.exp(my_list))  # --> Mais on ne peut pas faire grand chose dessus...

my_list_2 = [x * 0.1 for x in range(0, 10)]
print(my_list_2) # Super on a des floats
# print(math.exp(my_list_2))   # --> Mais ca n'est pas beaucoup mieux
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9]

4.2 Import numpy and play with it

numpy has been thought to deal with data generic operations.

In [14]:
import numpy as np

linspace uses the third argument to specify the number of points. The fourth argument "endpoint=True/False" permits to activate, or not, the right endpoint.

In [15]:
my_np_linspace = np.linspace(0.,1.,11)
print(my_np_linspace)
print(np.exp(my_np_linspace))
[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. ]
[1.         1.10517092 1.22140276 1.34985881 1.4918247  1.64872127
 1.8221188  2.01375271 2.22554093 2.45960311 2.71828183]

arange permits to specify the step

In [16]:
my_np_arange = np.arange(0.0, 1.0, 0.1)
print(my_np_arange)
print(np.exp(my_np_arange))
[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
[1.         1.10517092 1.22140276 1.34985881 1.4918247  1.64872127
 1.8221188  2.01375271 2.22554093 2.45960311]

... Find the other over-exciting mathematical functions from numpy.

4.3 Plot figures : matplotlib

In [17]:
import matplotlib.pyplot as plt
In [18]:
fig = plt.figure()
plt.plot(my_np_arange,np.exp(my_np_arange), color='green', marker='o', linestyle='dashed',linewidth=2, markersize=12)
fig.suptitle('Une belle exponentielle', fontsize=20)
plt.xlabel('x ensemble de départ', fontsize=18)
plt.ylabel('y ensemble d arrivee', fontsize=16)
Out[18]:
Text(0,0.5,'y ensemble d arrivee')